/*
 * Copyright 2015 William Wang.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package wxk.Bonus.FXML;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.WorkerStateEvent;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.control.Label;
import javafx.scene.control.MenuBar;
import javafx.scene.control.ProgressIndicator;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TableView;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Region;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import static wxk.Bonus.FXML.DoubleBallController.globalconfig;
import wxk.Bonus.utils.BonusLogger;
import wxk.Bonus.utils.MessageUtils;
import wxk.Bonus.utils.ScreenUtil;
import wxk.Bonus.Network.UnionLottoSync;
import wxk.Bonus.Network.UnionLottoStore;
/**
 * FXML Controller class
 *
 * @author William Wang
 */
public class SampleController implements Initializable {
BonusLogger mylog=BonusLogger.getInstance();
    private URL globalurl;
    private ResourceBundle globalrb;
    @FXML
    private Pane parentpane;//top parent Pane
    
    @FXML 
    private BorderPane mainBorderPane;
    @FXML private ScrollPane mainscrollpane;
    @FXML private VBox righttoolbar;
    @FXML private HBox bottomstatusbar;
    @FXML private MenuBar topmenubar;
    @FXML private Label statusbarlb;
    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        globalurl=url;
        globalrb=rb;
        
    checkAssert();
    adjustUISurface();  
    setGlobalConfig();//keep system ui.
         
    }    
    @FXML
    public void menuDataSyncData(ActionEvent event)
    {
     mylog.log("call sync data,call thread...");   
     globalconfig.updateSystemStatusBar("Read Data from Internet WebSite..."); 
     globalconfig.showWorking();
    UnionLottoStore uls=UnionLottoStore.getInstance();
    uls.initializeDB();//ready handler
    new Thread(uls).start();
    }
    @FXML
     public void menuCloseAction(ActionEvent event)
     {
         Platform.exit();
     }
     @FXML private ImageView superlotto;
     
     @FXML
     public void toolbarUnionLotto(MouseEvent event)
     {
         System.out.println(MessageUtils.geti18nMessage("UNION LOTTO CLICK"));
         try
         {
        URL  resource=SampleController.class.getResource("/wxk/Bonus/FXML/DoubleBall.fxml");
       FXMLLoader fxmlloader=new FXMLLoader(resource);
        GridPane root = fxmlloader.load();
        DoubleBallController dbcon = fxmlloader.getController();
        
       dbcon.modifyGridPane(mainscrollpane.getPrefWidth(), mainscrollpane.getPrefHeight());
  
      //1. 
       root.setPrefWidth(mainBorderPane.getPrefWidth());
       root.setPrefHeight(mainBorderPane.getPrefHeight());
      //2.
 
 
        mainBorderPane.setCenter(null);
//         mainBorderPane.setCenter(root);

            System.out.println(".........."+new java.util.Date());    
                 //3.read table
       globalconfig.updateSystemStatusBar("Read Data from Databse...");  
       globalconfig.showWorking();
       UnionLottoSync unionsync=new UnionLottoSync();
         //add testing
//         StackPane stack = new StackPane();
//         Region veil = new Region();
//        veil.setStyle("-fx-background-color: rgba(0, 0, 0, 0.4)");
//        ProgressIndicator p = new ProgressIndicator();
//        p.setMaxSize(150, 150);
//        p.progressProperty().bind(unionsync.progressProperty());
//        veil.visibleProperty().bind(unionsync.runningProperty());
//        p.visibleProperty().bind(unionsync.runningProperty());
//        dbcon.getTableView().itemsProperty().bind(unionsync.valueProperty());
//        stack.getChildren().addAll(veil, p);
//        root.getChildren().add(stack);
                 //add over       
  mainBorderPane.setCenter(root);
       new Thread(unionsync).start();  
unionsync.setOnSucceeded( 
        new EventHandler<WorkerStateEvent>() {

            @Override
            public void handle(WorkerStateEvent event) {
                System.out.println("auto scroll end .....");
                dbcon.getTableView().scrollTo(globalconfig.filterData.size());
            }
        });
            
         }catch(Exception e)
         {
             e.printStackTrace();
         }

       
     } 
     
     @FXML private ImageView unionlotto;
     @FXML
     public void toolbarSuperLotto(MouseEvent event)
     {
         System.out.println("super lotto click");
     }      
     
 public void checkAssert()
 {
    assert parentpane == null : MessageUtils.getFormatMessage("assert_msg", "parentpane");
     assert mainBorderPane == null : MessageUtils.getFormatMessage("assert_msg", "mainBorderPane");     
     assert mainscrollpane == null : MessageUtils.getFormatMessage("assert_msg", "mainscrollpane"); 
assert unionlotto == null : MessageUtils.getFormatMessage("assert_msg", "unionlotto");
assert superlotto == null : MessageUtils.getFormatMessage("assert_msg", "superlotto");
assert righttoolbar == null : MessageUtils.getFormatMessage("assert_msg", "righttoolbar");
assert bottomstatusbar == null : MessageUtils.getFormatMessage("assert_msg", "bottomstatusbar");
assert topmenubar == null : MessageUtils.getFormatMessage("assert_msg", "topmenubar");
assert statusbarlb == null : MessageUtils.getFormatMessage("assert_msg", "statusbarlb");
 }
     /**
      * adjust main ui width and height as max screen avaliable 
      */
     private void adjustUISurface()
     {
       mylog.log("call adjustUISurface....");
       mainBorderPane.setRight(null);
       //get menubar/statusbar height 
       double statusbarh = bottomstatusbar.getPrefHeight();
        double menubarh =  topmenubar.getPrefHeight();
        //get left toolbar width
        double lefttoolbar=mainBorderPane.getLeft().getBoundsInLocal().getWidth();
        
        ScreenUtil sutil=ScreenUtil.getInstance();
        int systemtaskbar=sutil.getTaskBarHeight();
        mainBorderPane.setPrefWidth(sutil.getAvaliable_screen_width());
        mainBorderPane.setPrefHeight(sutil.getAvaliable_screen_height()
                -systemtaskbar);
       System.out.println("mainscrollpane width:"+(sutil.getAvaliable_screen_width()-lefttoolbar));
        mainscrollpane.setPrefWidth(sutil.getAvaliable_screen_width()-lefttoolbar);  
        //set right tool bar height
        righttoolbar.setPrefHeight(sutil.getAvaliable_screen_height()-statusbarh-menubarh
                -sutil.getTitlebar_height());
        //set bottom status bar width
        statusbarlb.setPrefWidth(sutil.getAvaliable_screen_width());
     }
  public void setGlobalConfig()
  {
      globalconfig.setStatusbar(statusbarlb);
      globalconfig.setBottomstatusbar(bottomstatusbar);   
  }
}
